
project , uses(../data_output/legislators_klarner.dta)


	**********
	** ARKANSAS
	** Life-time term limites: 3x2 year terms (after 2016: 8 x 2-year terms.)
	** First year: 1998	
	**********	
	** Arkansas General Assembly: sixteen years total in either the House or the Senate. Prior to the 2016 election (on the ballot in 2014), the previous limits of three two-year terms for House members (six years) and two four-year terms for Senate members (eight years) applied
	
	** Arkansas prep Klarner
	use ../data_output/legislators_klarner.dta, clear
	gen mergename =  regexs(1) if regexm(lower(CandName),"([a-z]+),")
	keep if state=="AR" & chamber=="house"
	keep if regexm(term, "1987|1989|1991|1993|1995|1997|1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen lastname = regexs(1) if regexm(lower(CandName) ,"(.*),")
	gen first = regexs(1) if regexm(lower(CandName) ,", ([a-z])")
	bys mergename term: gen n= _n
	bys mergename term: egen tmp = mean(n)
	replace mergename = first + ". " + lastname if tmp>1
	replace mergename = first + ". " + lastname if regexm(lastname, "smith")
	replace mergename = lower("JACQUELINE ROBERTS") if CandId == 9414 & term=="1991-1992"
	replace mergename = "m.j. gray" if CandId == 322110 & term=="2015-2016"
	drop n tmp first lastname	
	save ../data_output/legislators_AR_house_1987_2016.dta, replace
	
	
	
	gen e_won = elections_won
	qui forval i= 1960(2)1990{ //years enacted
		replace e_won = subinstr(e_won, "`i';", "",.)
	}
	tostring year, gen(regexp)
	replace regexp = "^([^!]+"+regexp+";)"
	gen previous_wins = regexs(1) if regexm(e_won, regexp)
	egen prev_terms = noccur(previous_wins) , string(";")
	gen termlimited=prev_terms==3	
	save ../data_output/legislators_AR_house_1987_2016.dta, replace
project ,  creates("../data_output/legislators_AR_house_1987_2016.dta")
	
	
	clear
	
	tempfile tmp
	gen bill=""
	save `tmp'

	local files : dir "../data_input/AR/" files "AR_*.txt"
	qui foreach file in `files' {
project , original(../data_input/AR/`file')
		import delimited using ../data_input/AR/`file',  clear   delimiters("\t")  encoding("utf-8") varnames(nonames)
		keep v1 v2 v3 v4
		rename v1 bill
		rename v2 subject
		rename v3 act
		rename v4 sponsor
		replace bill = trim(bill)
		tostring bill subject act sponsor, replace
		gen year = regexs(1) if regexm("`file'","AR_([12][0-9][0-9][0-9])_")
		gen inoffice = year
		gen f = "`file'"
		destring year, replace	
		append using `tmp'
		save `tmp', replace
	}

	drop f
	
	gen mergename = sponsor
	drop if regexm(lower(mergename),"management|&|comm" )
	replace mergename = trim(usubinstr(mergename, "ET AL","",.))
	replace mergename = lower(mergename)	
		
	gen term = ""
	replace term = "1987-1988" if regexm(inoffice, "1987|1988")
	replace term = "1989-1990" if regexm(inoffice, "1989|1990")
	replace term = "1991-1992" if regexm(inoffice, "1991|1992")
	replace term = "1993-1994" if regexm(inoffice, "1993|1994")
	replace term = "1995-1996" if regexm(inoffice, "1995|1996")	
	replace term = "1997-1998" if regexm(inoffice, "1997|1998")
	replace term = "1999-2000" if regexm(inoffice, "1999|2000")
	replace term = "2001-2002" if regexm(inoffice, "2001|2002")
	replace term = "2003-2004" if regexm(inoffice, "2003|2004")
	replace term = "2005-2006" if regexm(inoffice, "2005|2006")
	replace term = "2007-2008" if regexm(inoffice, "2007|2008")
	replace term = "2009-2010" if regexm(inoffice, "2009|2010")
	replace term = "2011-2012" if regexm(inoffice, "2011|2012")
	replace term = "2013-2014" if regexm(inoffice, "2013|2014")
	replace term = "2015-2016" if regexm(inoffice, "2015|2016")
	
	replace mergename = usubinstr(mergename, "representative", "",.)
	
	replace mergename = "jordon" if mergename=="jordan"
	replace mergename = "walker" if mergename=="w. walker"
	replace mergename = "baker" if mergename=="t. baker"
	replace mergename = "bradford" if mergename=="t. bradford"
	replace mergename = "bradford" if mergename=="t. bradford"
	replace mergename = "edwards" if mergename=="j. edwards"
	replace mergename = "collinssmith" if mergename=="collins-smith"
	replace mergename = "cowling" if mergename=="l. cowling"
	replace mergename = "green" if mergename=="r. green"
	replace mergename = "creekmore" if mergename=="d. creekmore"
	replace mergename = "hutchinson" if mergename=="d. hutchinson"
	replace mergename = "whitaker" if mergename=="d. whitaker"
	replace mergename = "malone" if mergename=="s. malone"
	replace mergename = "malone" if mergename=="p. malone"
	replace mergename = "brown" if mergename=="j. brown" & regexm(term, "2009|2011")
	replace mergename = "c. brown" if mergename=="brown" & regexm(term, "1987")
	replace mergename = "dickinson" if mergename=="j. dickinson"
	replace mergename = "lewellen" if mergename=="w. lewellen" | mergename=="j. lewellen"
	replace mergename = "martin" if mergename=="m. martin" & regexm(term, "2007|2009")
	replace mergename = "roebuck" if mergename=="j. roebuck"
	replace mergename = "overbey" if mergename=="b. overbey"
	replace mergename = "burris" if mergename=="j. burris" & regexm(term, "2011|2013")
	replace mergename = "prater" if mergename=="s. prater" & regexm(term, "2005|2007")
	replace mergename = "altes" if mergename=="d. altes"
	replace mergename = "steele" if mergename=="t. steele" & regexm(term, "2011")
	replace mergename = "wagner" if mergename=="w. wagner"
	replace mergename = "thompson" if mergename=="t. thompson" & regexm(term, "2011|2013")
	replace mergename = "vongremp" if mergename=="von gremp"
	replace mergename = "j. smith" if mergename=="smith" & regexm(term, "1991|1993")
	replace mergename = "d. miller" if mergename=="s. miller"
	replace mergename = "b. hendrix" if mergename=="hendrix"
	replace mergename = "flechter" if mergename=="fletcher"
	replace mergename = "bookout" if mergename=="p. bookout"
	replace mergename = "dellarosa" if mergename=="della rosa"
	replace mergename = "dobbins" if mergename=="s. dobbins"
	replace mergename = "l. collinssmith" if mergename=="collinssmith" 
	replace mergename = "flowers" if mergename=="v. flowers"
	replace mergename = "jeffress" if mergename=="g. jeffress"
	replace mergename = "hendren" if mergename=="k. hendren"
	replace mergename = "hendrix" if mergename=="b. hendrix" & regexm(term, "1987|1989|1991|1993")
	replace mergename = "owensingram" if mergename=="ingram" & regexm(term, "1997")
	replace mergename = "j. wilson" if mergename=="wilson" & regexm(term, "1991")
	replace mergename = "johnson" if mergename=="c. johnson" & regexm(term, "2001")
	replace mergename = "mahoney" if mergename=="mahony" & regexm(term, "1993")
	replace mergename = "mayberry" if mergename=="j. mayberry"
	replace mergename = "mcgill" if mergename=="g. mcgill"
	replace mergename = "owensingram" if mergename=="owens"
	replace mergename = "rogers" if mergename=="t. rogers" & regexm(term, "2011")
	replace mergename = "wilkins" if mergename=="b. wilkins" & regexm(term, "2009")
	replace mergename = "flanagin" if regexm(mergename,"flangin|flanigan|flannigan")
	replace mergename = "mcgehee" if mergename=="mcgeheee"

		

	
	merge m:1 mergename term using ../data_output/legislators_AR_house_1987_2016.dta
	**43 legislators sponsor 0 bills in a particular session
	drop if _merge==1
	drop _merge
	drop sponsor
	gen sponsor = 1 if bill!=""
	gen law = act!=""
	save ../data_output/bills_by_sponsor_AR_house_1987_2016.dta, replace
project ,  creates("../data_output/bills_by_sponsor_AR_house_1987_2016.dta")

	use ../data_output/bills_by_sponsor_AR_house_1987_2016.dta, clear
	
	collapse (sum) sponsor law (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party mergename inoffice year termlimited vtsh, by(CandId term )
	
	save ../data_output/total_bills_by_sponsor_AR_house_1987_2016.dta, replace
project ,  creates("../data_output/total_bills_by_sponsor_AR_house_1987_2016.dta")


	
	
	
	**Voting
	use ../data_output/legislators_klarner.dta, clear
	gen lastname = regexs(1) if regexm(lower(CandName),"([^,]+), [^!]")
	gen firstletter = regexs(1) if regexm(lower(CandName),"[^,]+, ([^!])")
	gen secondletter = regexs(1) if regexm(lower(CandName),"[^,]+, ([^!][^!])")
	keep if state=="AR" & chamber=="house"
	keep if regexm(term, "2001|2003|2005|2007|2009|2011|2013|2015")
	gen os_mergename = lastname
	bys  os_mergename term: gen n = _n
	by  os_mergename term: egen mean_n=mean(n)
	replace os_mergename = firstletter+". " +lastname  if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	bys  os_mergename term: gen n = _n
	by  os_mergename term: egen mean_n=mean(n)
	replace os_mergename = CandName  if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	tempfile tmp
	save `tmp', replace
		

	
	import delimited using ../data_input/AR/ar_votes.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	keep if chamber=="house"
	gen term = ""
	replace term = "2001-2002" if regexm(session, "2001|2002")
	replace term = "2003-2004" if regexm(session, "2003|2004")
	replace term = "2005-2006" if regexm(session, "2005|2006")
	replace term = "2007-2008" if regexm(session, "2007|2008")
	replace term = "2009-2010" if regexm(session, "2009|2010")
	replace term = "2011-2012" if regexm(session, "2011|2012")
	replace term = "2013-2014" if regexm(session, "2013|2014")
	replace term = "2015-2016" if regexm(session, "2015|2016")	
	gen os_mergename = lower(name)
	replace os_mergename = lower(os_mergename)
	replace os_mergename = subinstr(os_mergename,"’","",.)
	replace os_mergename = subinstr(os_mergename,"'","",.)
	replace os_mergename = subinstr(os_mergename,"-","",.)	
	replace os_mergename = "overbey"	if os_mergename=="b. overbey" & regexm(term, "2013|2015")
	replace os_mergename = "lewellen"	if os_mergename=="w. lewellen" & regexm(term, "2007|2009")
	replace os_mergename = "johnson"	if os_mergename=="c. johnson" & regexm(term, "2001")
	replace os_mergename = "burris"	if os_mergename=="j. burris" & regexm(term, "2013")
	replace os_mergename = "brown"	if os_mergename=="j. brown" & regexm(term, "2009")
	replace os_mergename = "hutchinson"	if os_mergename=="d. hutchinson" & regexm(term, "2007|2009")
	replace os_mergename = "malone"	if os_mergename=="s. malone" & regexm(term, "2009|2013")
	replace os_mergename = "dellarosa"	if os_mergename=="della rosa" & regexm(term, "2015")
	replace os_mergename = "dickinson"	if os_mergename=="j. dickinson" & regexm(term, "2009|2013")
	replace os_mergename = "dobbins"	if os_mergename=="s. dobbins" & regexm(term, "2007")
	replace os_mergename = "roebuck"	if os_mergename=="j. roebuck" & regexm(term, "2007|2009")
	replace os_mergename = "martin"	if os_mergename=="m. martin" & regexm(term, "2007|2009")
	replace os_mergename = "mayberry"	if os_mergename=="j. mayberry" & regexm(term, "2015")
	replace os_mergename = "mcgill"	if os_mergename=="g. mcgill" & regexm(term, "2015")
	replace os_mergename = "flowers"	if os_mergename=="v. flowers" & regexm(term, "2015")
	replace os_mergename = "wagner"	if os_mergename=="w. wagner" & regexm(term, "2013")
	replace os_mergename = "walker"	if os_mergename=="w. walker" & regexm(term, "2001")
	replace os_mergename = "whitaker"	if os_mergename=="d. whitaker" & regexm(term, "2013|2015")
	replace os_mergename = "edwards"	if os_mergename=="j. edwards" & regexm(term, "2009|2013")
	replace os_mergename = "prater"	if os_mergename=="s. prater" & regexm(term, "2007")
	replace os_mergename = "smith"	if os_mergename=="r. smith" & regexm(term, "2003")
	replace os_mergename = "smith"	if os_mergename=="l. smith" & regexm(term, "2005|2007")
	replace os_mergename = "smith"	if os_mergename=="b. smith" & regexm(term, "2015")
	replace os_mergename = "cowling"	if os_mergename=="l. cowling" & regexm(term, "2007|2009")
	replace os_mergename = "thompson"	if os_mergename=="t. thompson" & regexm(term, "2013")
	replace os_mergename = "wilkins"	if os_mergename=="b. wilkins" & regexm(term, "2009")
	replace os_mergename = "jeffress"	if os_mergename=="g. jeffress" & regexm(term, "2001")
	replace os_mergename = "bookout"	if os_mergename=="p. bookout" & regexm(term, "2003")
	replace os_mergename = "bradford"	if os_mergename=="t. bradford" & regexm(term, "2007|2009")
	replace os_mergename = "creekmore"	if os_mergename=="d. creekmore" & regexm(term, "2007|2009")
	replace os_mergename = "green"	if os_mergename=="r. green" & regexm(term, "2007|2009")
	replace os_mergename = "baker"	if os_mergename=="t. baker" & regexm(term, "2009")
	replace os_mergename = "altes"	if os_mergename=="d. altes" & regexm(term, "2013")
	replace os_mergename = "GRAY, MICHAEL JOHN"	if os_mergename=="m.j. gray" & regexm(term, "2015")
	replace os_mergename = "GRAY, MICHELLE"	if os_mergename=="m. gray" & regexm(term, "2015")
	replace os_mergename = "hendren"	if os_mergename=="k. hendren" & regexm(term, "2015")
		
	merge m:1  os_mergename term using `tmp'	
	
	// 12 obs not merging
	drop if _merge==1
	drop _merge	
	
	replace vote = "1" if vote=="yes"
	replace vote = "-1" if vote=="no"
	replace vote = "0" if regexm(vote,"1")==0
	gen floorvotes = regexm(vote, "1")
	destring vote, replace
	egen vote_id = group(session chamber bill date location) , missing
	bys party vote_id : egen party_mean=mean(vote)
	gen party_vote = 0
	replace party_vote = 1 if party_mean>0
	replace party_vote = -1 if party_mean<0
	gen vote_party_line = vote==party_vote
	gen vote_against_party = vote==-party_vote
	bys vote_id: egen votemargin = mean(vote)
	gen close_floorvotes = floorvotes if abs(votemargin)<.1		
	
	save ../data_output/floorvotes_AR_2001_2016.dta, replace
project ,  creates("../data_output/floorvotes_AR_2001_2016.dta")

	use ../data_output/floorvotes_AR_2001_2016.dta, clear
	collapse  (mean) vote_party_line vote_against_party (sum)  *floorvotes (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party  inoffice year  vtsh, by(CandId term )
	
	save ../data_output/total_floorvotes_AR_2001_2016.dta, replace
project ,  creates("../data_output/total_floorvotes_AR_2001_2016.dta")

	
	
	
